import axios from 'axios';
axios.defaults.baseURL="/api";
// 登录请求方法
const loginreq = (method, url, params) => {
    //alert("登录")
    return axios({
        method: method,
        url: url,
        // headers: {
        //     'Content-Type': 'application/x-www-form-urlencoded',
        // },
        data: params,
        // traditional: true,
        // transformRequest: [
        //     function(data) {
        //         let ret = ''
        //         for (let it in data) {
        //             ret +=
        //                 encodeURIComponent(it) +
        //                 '=' +
        //                 encodeURIComponent(data[it]) +
        //                 '&'
        //         }
        //         return ret
        //     }
        // ]
    }).then(res => res.data);
};
// 通用公用方法
const req = (method,url,params) => {
  console.info("token:"+localStorage.getItem('logintoken'))
    return axios({
        method: method,
        url: url,
        headers: {
          /*contentType: "application/json;charset=UTF-8",*/
          'Content-Type': 'application/x-www-form-urlencoded',
          token: localStorage.getItem('logintoken')
        },
        data: params,
        traditional: true,
        transformRequest: [
            function(data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
// 通用公用方法
const reqJSON = (method,url,params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type':'application/json;charset=UTF-8',
      token: localStorage.getItem('logintoken')
    },
    data: params,
  }).then(res => res.data);
};
const reqForFile = (method,url,params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      contentType: "multipart/form-data",
      'Content-type' : 'multipart/form-data',
      token: localStorage.getItem('logintoken')
    },
    data: params,
    traditional: true,
    transformRequest: [
      function(data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};

export {
    loginreq,
    req,
    reqJSON,
    reqForFile
}
